9350b6
@@ -140,6 +140,12 @@
public static ColumnInfo resolveColumn(RowSchema rs,
       .add(PrimitiveCategory.VARCHAR)
       .add(PrimitiveCategory.CHAR).build();
 
+    private static final Set<PrimitiveCategory> unsafeConversionTypes = ImmutableSet
+      .<PrimitiveCategory>builder()
+      .add(PrimitiveCategory.STRING)
+      .add(PrimitiveCategory.VARCHAR)
+      .add(PrimitiveCategory.CHAR).build();
+
   /**
    * Cast type from expression type to expected type ti.
    *
@@ -148,6 +154,19 @@
public static ColumnInfo resolveColumn(RowSchema rs,
    * @return cast constant, or null if the type cast failed.
    */
   private static ExprNodeConstantDesc typeCast(ExprNodeDesc desc, TypeInfo ti) {
+    return typeCast(desc, ti, false);
+  }
+
+  /**
+   * Cast type from expression type to expected type ti.
+   *
+   * @param desc constant expression
+   * @param ti expected type info
+   * @param performSafeTypeCast when true then don't perform typecast because it could be unsafe (loosing leading zeroes etc.)
+   * @return cast constant, or null if the type cast failed.
+   */
+
+  private static ExprNodeConstantDesc typeCast(ExprNodeDesc desc, TypeInfo ti, boolean performSafeTypeCast) {
     if (desc instanceof ExprNodeConstantDesc && null == ((ExprNodeConstantDesc)desc).getValue()) {
       return null;
     }
@@ -164,6 +183,17 @@
private static ExprNodeConstantDesc typeCast(ExprNodeDesc desc, TypeInfo ti) {
       // ExprNodeConstantDesc
       return null;
     }
+
+    // We shouldn't cast strings to other types because that can broke original data in cases of
+    // leading zeros or zeros trailing after decimal point.
+    // Example: "000126" => 126 => "126"
+
+    boolean brokingDataTypesCombination = (unsafeConversionTypes.contains(priti.getPrimitiveCategory()) &&
+            !unsafeConversionTypes.contains(descti.getPrimitiveCategory()));
+    if (performSafeTypeCast && brokingDataTypesCombination) {
+      return null;
+    }
+
     if (LOG.isDebugEnabled()) {
       LOG.debug("Casting " + desc + " to type " + ti);
     }
@@ -502,7 +532,7 @@
private static void propagate(GenericUDF udf, List<ExprNodeDesc> newExprs, RowSc
           LOG.debug("Filter " + udf + " is identified as a value assignment, propagate it.");
         }
         if (!v.getTypeInfo().equals(ci.getType())) {
-          v = typeCast(v, ci.getType());
+          v = typeCast(v, ci.getType(), true);
         }
         if (v != null) {
           constants.put(ci, v);
